package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxEightDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.BoxEight;
import com.example.shiwu.db.model.BoxOne;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxEightService {

    private DatabaseHelper dbHelper;
    private BoxEightDao<BoxEight> BoxEightDao;
    private ArrayList<BoxEight> listBoxEight = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxEightService() {
    }

    public BoxEightService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }




    /**获取单个数据
     * @param BoxEight
     * @return
     */
    public List<BoxEight> getBoxEight(BoxEight BoxEight) {
        BoxEightDao = BaseDaoFactory.getInstance().createBaseDao(BoxEightDao.class, BoxEight.class);
        listBoxEight = new ArrayList<>();
        listBoxEight.addAll(BoxEightDao.query(BoxEight));
        return listBoxEight;
    }

    /**分页获取数据
     * @param BoxEight
     * @return
     */
    public List<BoxEight> getBoxEightList(BoxEight BoxEight,Integer startIndex,Integer limit) {
        BoxEightDao = BaseDaoFactory.getInstance().createBaseDao(BoxEightDao.class, BoxEight.class);
        listBoxEight = new ArrayList<>();
        listBoxEight.addAll(BoxEightDao.query(BoxEight,null,"create_time desc",null,startIndex,limit));
        return listBoxEight;
    }


    public boolean save(BoxEight BoxEight) {
        BoxEightDao = BaseDaoFactory.getInstance().createBaseDao(BoxEightDao.class, BoxEight.class);
        long size = BoxEightDao.insert(BoxEight);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxEight BoxEight) {
        BoxEightDao = BaseDaoFactory.getInstance().createBaseDao(BoxEightDao.class, BoxEight.class);
        BoxEight boxEight = new BoxEight();
        boxEight.setItemId(BoxEight.getItemId());
        List<BoxEight> lis =BoxEightDao.query(boxEight);
        Boolean bl = true;
        if(lis.size()>0){
            BoxEight.setUpdateTime(DateUtils.parseDate());
            long size = BoxEightDao.update(BoxEight,new BoxEight());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            BoxEight.setCreateTime(DateUtils.parseDate());
            long size = BoxEightDao.insert(BoxEight);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxEight BoxEight,BoxEight where) {
        long size = BoxEightDao.update(BoxEight,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Boolean delete(BoxEight boxEight) {
        int size = BoxEightDao.delete(boxEight);
        if(size >0 ){
            return true;
        }
        return false;
    }

}
